package com.example.administrator.application;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    EditText name,number;
    ListView listView;
    Button add,query;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        add = (Button) findViewById(R.id.button);
        query = (Button) findViewById(R.id.button1);
        name=(EditText)findViewById(R.id.editText3);
        number=(EditText)findViewById(R.id.editText4);
        listView=(ListView)findViewById(R.id.listView);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent addIntent=new Intent(Intent.ACTION_INSERT, Uri.withAppendedPath(Uri.parse("content://com.android.contacs"),"contacts"));
                addIntent.setType("vnd.android.cursor.dir/person");
                addIntent.setType("vnd.android.cursor.dir/contact");
                addIntent.setType("vnd.android.cursor.dir/raw_contact");
                addIntent.putExtra(android.provider.ContactsContract.Intents.Insert.NAME, name.getText());
                addIntent.putExtra(android.provider.ContactsContract.Intents.Insert.PHONE, number.getText());
                startActivity(addIntent);
            }
        });
      query.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              ContentResolver contentResolver=MainActivity.this.getContentResolver();
              Cursor cursor=contentResolver.query(ContactsContract.Contacts.CONTENT_URI,new String[] { ContactsContract.Contacts._ID,
                      ContactsContract.Contacts.DISPLAY_NAME },null,null,null);
              List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
              while (cursor.moveToNext()){
                  int inIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);
                  int id = cursor.getInt(inIndex);
                  int nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
                  String displayName = cursor.getString(nameIndex);
                  Cursor phone = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI
                          ,null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+id,null,null);
                  String phoneNumber=null;
                  while (phone.moveToNext()){
                      int phoneNumberIndex = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                      phoneNumber = phone.getString(phoneNumberIndex);
                  }
                  Map<String, Object> item = new HashMap<String, Object>();
                  item.put("id",id);
                  item.put("name",displayName);
                  item.put("phoneNumber",phoneNumber);
                  items.add(item);

              }

              SimpleAdapter adapter=new SimpleAdapter(MainActivity.this,
                      items,
                      R.layout.layout_list_item,
                      new String[]{"id","name","phoneNumber"},
                      new int[] {R.id.tetId,R.id.tetName,R.id.tetPhone}
              );
              listView.setAdapter(adapter);
          }
      });
    }

//    public void showPeople(View view){
//        switch (view.getId()){
//            case R.id.button:
//                Intent addIntent=new Intent(Intent.ACTION_INSERT, Uri.withAppendedPath(Uri.parse("content://com.android.contacs"),"contacts"));
//                addIntent.setType("vnd.android.cursor.dir/person");
//                addIntent.setType("vnd.android.cursor.dir/contact");
//                addIntent.setType("vnd.android.cursor.dir/raw_contact");
//                addIntent.putExtra(android.provider.ContactsContract.Intents.Insert.NAME, name.getText());
//                addIntent.putExtra(android.provider.ContactsContract.Intents.Insert.PHONE, number.getText());
//                startActivity(addIntent);
//                break;
//            case R.id.button1:
//                ContentResolver contentResolver=MainActivity.this.getContentResolver();
//                Cursor cursor=contentResolver.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
//                List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
//                while (cursor.moveToNext()){
//                    int inIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);
//                    int id = cursor.getInt(inIndex);
//                    int nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
//                    String displayName = cursor.getString(nameIndex);
//                    Cursor phone = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI
//                            ,null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+id,null,null);
//                    String phoneNumber=null;
//                    while (phone.moveToNext()){
//                        int phoneNumberIndex = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
//                        phoneNumber = phone.getString(phoneNumberIndex);
//                    }
//                    Map<String, Object> item = new HashMap<String, Object>();
//                    item.put("id",id);
//                    item.put("name",displayName);
//                    item.put("phoneNumber",phoneNumber);
//                    items.add(item);
//
//                }
//
//                SimpleAdapter adapter=new SimpleAdapter(MainActivity.this,
//                        items,
//                        R.layout.layout_list_item,
//                        new String[]{"id","name","phoneNumber"},
//                        new int[] {R.id.tetId,R.id.tetName,R.id.tetPhone}
//                );
//                listView.setAdapter(adapter);
//        }
//
//    }

}